package yzserver.sx4a.org;

import com.sitech.portal.system.dao.ISysDeptDao;
import com.sitech.portal.system.domain.TblSYS_DEPT;
import yzserver.sx4a.RetTools;
import yzserver.sx4a.dao.IOrgDao;
import yzserver.sx4a.domain.OrganizationDomain;
import yzserver.sx4a.form.Organization;
import yzserver.sx4a.form.Ret;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: andrew
 * Date: 14-8-15
 * Time: 上午10:14
 * To change this template use File | Settings | File Templates.
 */
public class Sx4aOrgImpl implements Sx4aOrg {

    private IOrgDao orgDao;
    private ISysDeptDao sysDeptDao;

    public void setSysDeptDao(ISysDeptDao sysDeptDao) {
        this.sysDeptDao = sysDeptDao;
    }

    public void setOrgDao(IOrgDao orgDao) {
        this.orgDao = orgDao;
    }

    @Override
    public Organization selOrg(String orgid) {
        OrganizationDomain orgDomain  = orgDao.selOrg(orgid);
        if(orgDomain!=null){
            Organization org = new Organization(orgDomain);
            return org;
        }else{
            return null;
        }
    }

    @Override
    public Ret addOrg(Organization org) {
        try {
            orgDao.addOrg(new OrganizationDomain(org));
        } catch (Exception e) {
            return RetTools.getRet("创建组织失败", e);
        }

        //portal
        //4a中 组织对应portal部门的概念
        TblSYS_DEPT d = new TblSYS_DEPT();
        d.setDEPT_ID(org.getOrgid());
        d.setDEPT_DESC(org.getDisplayname());
        d.setDEPT_NAME(org.getOrgname());
        d.setSUPER_DEPT_ID(org.getOrgparent());
        try {
            sysDeptDao.add(d);
        } catch (Exception e) {
            return RetTools.getRet("创建组织失败", e);
        }
        return RetTools.getRet("创建组织成功", null);
    }

    @Override
    public Ret updOrg(Organization org) {
        try {
            orgDao.updOrg(new OrganizationDomain(org));
        } catch (Exception e) {
            return RetTools.getRet("修改组织失败", e);
        }

        //portal
        TblSYS_DEPT d = new TblSYS_DEPT();
        d.setDEPT_ID(org.getOrgid());
        d.setDEPT_DESC(org.getDisplayname());
        d.setDEPT_NAME(org.getOrgname());
        d.setSUPER_DEPT_ID(org.getOrgparent());
        try {
            sysDeptDao.update(d);
        } catch (Exception e) {
            return RetTools.getRet("修改组织失败", e);
        }
        return RetTools.getRet("修改组织成功", null);
    }

    @Override
    public Ret delOrg(String orgid) {
        try {
            orgDao.delOrg(orgid);
        } catch (Exception e) {
            return RetTools.getRet("删除组织失败", e);
        }
        //portal
        TblSYS_DEPT d = new TblSYS_DEPT();
        d.setDEPT_ID(orgid);
        try {
            sysDeptDao.delete(d);
        } catch (Exception e) {
            return RetTools.getRet("删除组织失败", e);
        }
        return RetTools.getRet("删除组织成功", null);
    }

    @Override
    public List selAllOrg(String orgid) {
        List<OrganizationDomain> OrgDomains = new ArrayList<OrganizationDomain>();
        List  orgList = new ArrayList();
        if("all".equalsIgnoreCase(orgid)){ // all为接口约定的固定值
            OrgDomains = orgDao.selAllOrg(orgid);
            if(OrgDomains!=null){
                for (int i = 0; i <OrgDomains.size(); i++) {
                    Organization org = new Organization(OrgDomains.get(i));
                    orgList.add(org);
                }
            }
        }
        return orgList;
    }

    @Override
    public Ret sendAllOrganization(List<yzserver.sx4a.org.Organization> organizationlist) {
        if(organizationlist!=null){
            for (int i = 0; i < organizationlist.size(); i++) {
                yzserver.sx4a.org.Organization org = organizationlist.get(i);
                try {
                    orgDao.addOrg(new OrganizationDomain(org));
                } catch (Exception e) {
                    return RetTools.getRet("创建组织失败", e);
                }

                //portal
                //4a中 组织对应portal部门的概念
                TblSYS_DEPT d = new TblSYS_DEPT();
                d.setDEPT_ID(org.getId());
                d.setDEPT_DESC(org.getDisplayname());
                d.setDEPT_NAME(org.getName());
                d.setSUPER_DEPT_ID(org.getParentid());
                try {
                    sysDeptDao.add(d);
                } catch (Exception e) {
                    return RetTools.getRet("创建组织失败", e);
                }
            }
        }
        return RetTools.getRet("创建组织成功", null);
    }

    @Override
    public Ret sendAllBPOrganization(List<yzserver.sx4a.org.Organization> organizationlist) {
        if(organizationlist!=null){
            for (int i = 0; i < organizationlist.size(); i++) {
                yzserver.sx4a.org.Organization org = organizationlist.get(i);
                try {
                    orgDao.addOrg(new OrganizationDomain(org));
                } catch (Exception e) {
                    return RetTools.getRet("创建组织失败", e);
                }

                //portal
                //4a中 组织对应portal部门的概念
                TblSYS_DEPT d = new TblSYS_DEPT();
                d.setDEPT_ID(org.getId());
                d.setDEPT_DESC(org.getDisplayname());
                d.setDEPT_NAME(org.getName());
                d.setSUPER_DEPT_ID(org.getParentid());
                try {
                    sysDeptDao.add(d);
                } catch (Exception e) {
                    return RetTools.getRet("创建组织失败", e);
                }
            }
        }
        return RetTools.getRet("创建组织成功", null);
    }

}
